home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #2 / Monster Media No. 2 (Monster Media)(1994).ISO / prog_gen / psp_io.zip / NUMVERT.ASM < prev    next >
Assembly Source File  |  1994-05-28  |  12KB  |  253 lines

  1. ;****************************************************************************
  2. ;                         N U M V E R T . A S M
  3. ;============================================================================
  4. ; Reads number input by the user and outputs it in decimal, hexadecimal and
  5. ; binary formats.
  6. ;---------------------------------------------------------------------------
  7. ; Compile with Borland Turbo Assembler v3.0
  8. ; Link    with Borland Turbo Linker v5.1
  9. ;         and the files psp.obj, charstr.obj, dosio.obj & mathio.obj.
  10. ;---------------------------------------------------------------------------
  11. ; Copyright (c) Simon Groes, 1994.
  12. ;****************************************************************************
  13.  
  14.         IDEAL
  15.         DOSSEG
  16.         MODEL   small
  17.         STACK   256
  18.  
  19.         LOCALS                          ; Allow local labels.
  20.  
  21. ;-----  INCLUDE FILES:
  22.         INCLUDE "common.inc"
  23.         INCLUDE "macro.inc"
  24.  
  25. ;-----  EQUATES:
  26. ;-----  CL stands for 'command line'.
  27. CL_NUM          =       00000001b       ; Indicates number supplied in
  28.                                         ;  command line.
  29. CL_DEC          =       00000010b       ; Indicates to convert to decimal.
  30. CL_HEX          =       00000100b       ; Indicates to convert to hex.
  31. CL_BIN          =       00001000b       ; Indicates to convert to binary.
  32. CL_YES          =       11110000b       ; Indicates command-line parameters.
  33.         DATASEG
  34.  
  35. exit_code       db      0
  36.  
  37. welcome         db      CR, LF, 'Welcome to the Number Converter'
  38.                 db      CR, LF, 'Copyright (c) by Simon Groes, 1994',NULL
  39.  
  40. hinWeis         db      CR, LF, CR, LF
  41.                 db      '** INSTRUCTIONS **',CR, LF
  42.                 db      '------------------',CR, LF
  43.                 db      'You may type in a decimal, hexadecimal or '
  44.                 db      'binary number.',CR, LF
  45.                 db      'If you type a hexadecimal number, '
  46.                 db      'type an ''H'' or ''h'' at the end',CR,LF 
  47.                 db      '(no spaces in between).',CR,LF
  48.                 db      'A hex number must be <= 4 characters long.',CR,LF
  49.                 db      'If you type a binary number, type a ''B'' or'
  50.                 db      ' ''b'' at the end.',CR,LF
  51.                 db      'A binary number must be <= 16 characters long.'
  52.                 db      CR,LF
  53.                 db      'Do not type any letter at the end of a '
  54.                 db      'decimal number.',CR,LF
  55.                 db      'A decimal number must be an integer between '
  56.                 db      '0 & 65535.',CR,LF
  57.                 db      NULL
  58.  
  59. inStrPrompt     db      CR,LF,'Enter a number: ',NULL
  60. outStrHex       db      CR,LF,'The number in hexadecimal is: ',NULL
  61. outStrBin       db      CR,LF,'The number in binary is: ',NULL
  62. outStrDec       db      CR,LF,'The number in decimal is: ',NULL
  63. errorStr        db      CR,LF,'Invalid number.',NULL
  64. retryStr        db      CR,LF,'Try another (Y/N) ? ', NULL
  65. inValChoice     db      CR,LF,'Invalid choice.',NULL
  66. paramErrStr     db      'Invalid parameter(s).', CR,LF,CR,LF,NULL
  67.  
  68. strBuf          db      18 DUP (0)
  69. strBufLen       =       $ - strBuf
  70.  
  71.         CODESEG
  72.  
  73. ;-----  Declare all EXTRN procedures here:
  74.         ;-----  From PSP.ASM:
  75.         EXTRN   PSP_Parameters:PROC, GetParamNumber:PROC, GetSwNumber:PROC
  76.         EXTRN   GetNextParam:PROC
  77.         ;-----  From CHARSTR.ASM:
  78.         EXTRN   charToUpper:proc, strCopy:PROC
  79.         ;-----  From DOSIO.ASM:
  80.         EXTRN   dosPutString:proc, dosGetString:proc, dosGetEchoChar:proc
  81.         ;-----  From MATHIO.ASM:
  82.         EXTRN   AsciiToBin:proc, BinToAscii:proc
  83.  
  84.  
  85. Start:
  86.         call    NumvertParams           ; Get command-line parameters in dl.
  87.                                         ; di -> strBuf, si -> cmdBuf
  88.         or      dh, dh                  ; No parameters or error (dh = 0) ?
  89.         jz      Default                 ; Yes: Continue default operation.
  90.         test    dl, CL_NUM              ; No : Number on command-line ?
  91.         jz      Prompt                  ; No : Prompt for user input.
  92.         jmp     NEAR     Convert        ; Yes: Convert number to binary.
  93. Default:
  94.         mov     di, OFFSET welcome      ; Welcome user to program.
  95.         call    dosPutString
  96.         mov     di, OFFSET hinWeis      ; Display instructions.
  97.         call    dosPutString
  98.  
  99. Prompt:
  100.         mov     di, OFFSET inStrPrompt  ; Prompt user to enter number.
  101.         call    dosPutString            
  102.         mov     di, OFFSET strBuf       ; Dest of user input.
  103.         mov     cl, strBufLen           ; Max # chars + <return>.
  104.         call    dosGetString            ; Get user input.
  105.         xor     cx, cx                  ; cx = 0.
  106.  
  107. ; Note: Numbers entered in hex format must end with 'H' or 'h'.
  108. ;       Numbers entered in binary format end with 'B' or 'b'.
  109.  
  110. Convert:
  111.         call    AsciiToBin              ; Convert to 16-bit integer in ax.
  112.         jc      InputError              ; If invalid number, error.
  113.  
  114. ConvertDec:
  115.         test    dl, CL_DEC              ; Convert to decimal?
  116.         jz      ConvertHex              ; Try hexadecimal.     
  117. ;-----  Output number in decimal:
  118.         mov     di, OFFSET outStrDec    ; 'The number in decimal is:'
  119.         call    dosPutString            
  120.         mov     di, OFFSET strBuf       ; di -> Buffer to hold number.
  121.         mov     bx, 10                  ; bx = base.
  122.         call    BinToAscii              ; Convert to ascii string.
  123.         call    dosPutString            ; Display number.
  124.  
  125. ConvertHex:
  126.         test    dl, CL_HEX              ; Convert to hexadecimal ?
  127.         jz      ConvertBin              ; No : Try binary.
  128. ;-----  Output number in hexadecimal:
  129.         mov     di, OFFSET outStrHex    ; Output number in hexadecimal.
  130.         call    dosPutString
  131.         mov     di, OFFSET strBuf       ; di -> Buffer to hold number.
  132.         mov     bx, 16                  ; bx = base.
  133.         call    BinToAscii              ; Convert to ascii string.
  134.         call    dosPutString            ; Display number.
  135.  
  136. ConvertBin:
  137.         test    dl, CL_BIN              ; Convert to binary ?
  138.         jz      Retry                   ; No : Continue program.
  139. ;-----  Output number in binary:
  140.         mov     di, OFFSET outStrBin    ; Output number in binary.
  141.         call    dosPutString
  142.         mov     di, OFFSET strBuf       ; di -> Buffer to hold number.
  143.         mov     bx, 2                   ; bx = base.
  144.         call    BinToAscii              ; Convert to ascii string.
  145.         call    dosPutString            ; Display number.
  146.         jmp     NEAR    Retry           ; End program.
  147. InputError:
  148.         mov     di, OFFSET errorStr     ; 'Invalid number.'
  149.         call    dosPutString
  150.         mov     [exit_code], 1          ; End program with error.
  151. Retry:
  152.         mov     di, OFFSET retryStr     ; Try another (Y/N) ?
  153.         call    dosPutString
  154.         call    dosGetEchoChar          ; Get user's answer in al reg.
  155.         call    charToUpper             ; Convert to uppercase.
  156.         cmp     al, 'Y'                 ; Yes ?
  157.         je      Prompt                  ; Get user input.
  158.         cmp     al, 'N'                 ; No ?
  159.         je      Return                  ; End program.
  160.         mov     di, OFFSET inValChoice  ; Else, 'Invalid choice.'
  161.         call    dosPutString
  162.         jmp     NEAR    Retry           ; Try again.
  163. Return:
  164.         mov     ah, 04Ch                ; DOS function: Exit program
  165.         mov     al, [exit_code]         ; Return exit-code value
  166.         int     21h                     ; Call DOS. Terminate program
  167.  
  168. ;===============================================================
  169. ; Procedure:    =*=NumvertParams=*=
  170. ;---------------------------------------------------------------
  171. ; Access:       Private - Available only to this file.
  172. ; Task:         Interpret command-line parameters of numvert program.
  173. ; Input:        none
  174. ; Output:       dl = flags indicating command-line parameters.
  175. ;               dh = -1 (FFh) if parameters, dh = 0 if no parameters.
  176. ;               di = si -> strBuf
  177. ; Registers:    none
  178. ;===============================================================
  179. PROC    NumvertParams
  180.  
  181.         call    PSP_Parameters          ; Command-line parameters
  182.                                         ;  copied to predefined buffer,
  183.                                         ;  ds = es -> dataseg,
  184.                                         ;  di = si -> param buffer.
  185.         jc      @@Error                 ; If error in reading command line
  186.                                         ;  in PSP, end with error.
  187.         xor     dx, dx                  ; Clear dx for flags.
  188.         call    GetParamNumber          ; ax = # parameters.
  189.         or      ax, ax                  ; Any parameters ?
  190.         jz      @@NoParams              ; No : Terminate program.
  191.         mov     cx, ax                  ; Yes: # parameters in cx.
  192.         xor     ax, ax                  ; Clear ax for later use.
  193.         mov     si, OFFSET strBuf       ; Store offset of strBuf in si.
  194. ;---------------------------------------------------------------
  195. ; Note: Parameter # can consist of 1 positive integer, and up to
  196. ; 3 conversion choices - /b(binary), /h(hexadecimal), /d(decimal).
  197. ; The source number must be appended with the appropriate letter,
  198. ; indicating its type.  If no parameters are entered it is assumed
  199. ; the user wishes to convert to dec, hex & bin.  If no number is
  200. ; entered the user will be prompted in the main program.
  201. ;---------------------------------------------------------------
  202. ;-----  Enter loop here:
  203.         ASSUME  ds:@data, es:@data
  204. @@R1:   cmp     [BYTE di], SW1          ; Is param a /switch ?
  205.         je      @@SW
  206.         cmp     [BYTE di], SW2          ; Is param a -switch ?
  207.         je      @@SW
  208.                                         ; Must be a number to convert.
  209.         test    dl, CL_NUM              ; Only 1 number can be entered.
  210.         jnz     @@Error                 ; If already number entered, error.
  211.         xchg    di, si                  ; Else, swap di & si.
  212.         call    strCopy                 ; Number copied into buffer.
  213.         xchg    di, si                  ; Restore di & si registers.
  214.         or      dl, CL_NUM              ; Set 'number' flag.
  215.         jmp     NEAR    @@LR1           ; Loop back to @@R1.
  216. @@SW:   mov     al, [BYTE es:di + 1]    ; Move letter into al.
  217.         call    charToUpper             ; Convert to upper case.
  218.         cmp     al, 'D'                 ; Is it decimal ?
  219.         jne     @@H                     ; No : try hex.
  220.         or      dl, CL_DEC              ; Yes: set decimal flag.
  221.         jmp     NEAR    @@LR1           ; Jump to end of loop.
  222. @@H:    cmp     al, 'H'                 ; Is it hexadecimal ?
  223.         jne     @@B                     ; No : try binary.
  224.         or      dl, CL_HEX              ; Yes: set hexadecimal flag.
  225.         jmp     NEAR    @@LR1           ; Jump to end of loop.
  226. @@B:    cmp     al, 'B'                 ; Is it binary ?
  227.         jne     @@Error                 ; No : must be error.
  228.         or      dl, CL_BIN              ; Yes: set binary flag.
  229. @@LR1:  call    GetNextParam            ; di -> next parameter in cmdBuf.
  230.         jc      @@Error                 ; Unable to get next parameter.
  231.         loop    @@R1                    ; dec cx; or cx,cx; jne @@R1.
  232.  
  233. ;-----  Successful completion of the loop - cx = 0.
  234.         or      dh, CL_YES              ; Command-line parameters exist.
  235.         test    dl, CL_DEC OR CL_HEX OR CL_BIN  ; Any switch flags set ?
  236.         jnz     @@Return                        ; Yes: End program.
  237.         or      dl, CL_DEC OR CL_HEX OR CL_BIN  ; No : set all switches.
  238.  
  239. @@Return:
  240.         mov     di, si                  ; di -> number in strBuf.
  241.         ret                             ; Return to caller.
  242. @@Error:
  243.         mov     di, OFFSET paramErrStr  ; 'Invalid parameter(s).'
  244.         call    dosPutString
  245.         clc                             ; Clear carry flag.
  246. @@NoParams:
  247.         or      dl, CL_DEC OR CL_HEX OR CL_BIN  ; Set all switches.
  248.         jmp     NEAR    @@Return                ; End program.
  249. ENDP    NumvertParams
  250.  
  251.  
  252.         END     Start           ; End program / entry point.
  253.